<?php
/**
 * @Author:      zwh
 * @DateTime:    2017-3-21 16:58:30
 * @Description: 用于订单活动包操作
 */
class ScsProductActivityController extends BaseController {    
    /**
     * @Author:      zwh
     * @DateTime:    2017-3-21 13:47:22
     * @Description: 活动包列表
     */
    public function product_activity_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $name=Input::get('name');
            $pa=ProductActivity::leftJoin('product_activity_shop','product_activity.id','=','product_activity_shop.product_activity_id')
            ->where("product_activity_shop.shop_id",$sc_user->shop_id)
            ->where("product_activity.status",1)
            ->where("product_activity.is_member",0)
            ->where("product_activity.begin_time","<",Common::currentTimeString())
            ->where("product_activity.end_time",">",Common::currentTimeString());
            if($name!=""){
                $pa->where('name','like',"%".Input::get('name')."%");
            }
            $pa=$pa->select('product_activity.id','product_activity.name')->orderBy('product_activity.id')->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array('product_activity'=>$pa));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "product_activity_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-03-09T15:10:57+0800
     * @return   [type]                   [description]
     * 用户的会员包信息
     */
    public function member_product_activity_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $name=Input::get('name');
            //寻找会员包
            $user=Users::find(Input::get('user_id'));
            $now_member=MemberOrdinary::find($user->member);
            if(is_null($now_member)){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array('product_activity'=>array()));
                break;
            }
            $product_id=array();
            $mos=MemberOrdinaryServices::where("member_id",$now_member->id)->where("status",1)->get();
            foreach ($mos as $_mos) {
                $product_id[]=$_mos->package_id;
            }
            $mtu=MemberTemporaryUser::leftJoin("member_temporary","member_temporary.id","=","member_temporary_user.member_temporary_id")
                ->leftJoin("member_temporary_services","member_temporary_services.member_id","=","member_temporary.id")
                ->where("member_temporary_user.user_id",$user->id)->where("member_temporary_user.end_time",">",Common::currentTimeString())->select("member_temporary_services.package_id")->get();
            foreach ($mtu as $_mtu) {
                $product_id[]=$_mtu->package_id;
            }   
            $pa=ProductActivity::leftJoin('product_activity_shop','product_activity.id','=','product_activity_shop.product_activity_id')
            ->whereIn("product_activity.id",$product_id)
            ->where("product_activity_shop.shop_id",$sc_user->shop_id)
            ->where("product_activity.status",1)
            ->where("product_activity.is_member",1);
            if($name!=""){
                $pa->where('name','like',"%".Input::get('name')."%");
            }
            $pa=$pa->select('product_activity.id','product_activity.name')->orderBy('product_activity.id')->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array('product_activity'=>$pa));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "product_activity_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-3-21 17:02:01
     * @Description: 用于添加 订单活动包
     */
    public function addProductActivity(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'id'       => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40022");
                break;
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            $pa = ProductActivity::find(Input::get('id'));
            if($pa->is_member==1){//如果是会员包 判断是不是能用
                $user=Users::find($orders->user_id);
                $now_member=MemberOrdinary::find($user->member);
                $product_id=array();
                $mos=MemberOrdinaryServices::where("member_id",$now_member->id)->where("status",1)->get();
                foreach ($mos as $_mos) {
                    $product_id[]=$_mos->package_id;
                }
                $mtu=MemberTemporaryUser::leftJoin("member_temporary","member_temporary.id","=","member_temporary_user.member_temporary_id")
                    ->leftJoin("member_temporary_services","member_temporary_services.member_id","=","member_temporary.id")
                    ->where("member_temporary_user.user_id",$user->id)->where("member_temporary_user.end_time",">",Common::currentTimeString())->select("member_temporary_services.package_id")->get();
                foreach ($mtu as $_mtu) {
                    $product_id[]=$_mtu->package_id;
                } 
                if(!in_array($pa->id,$product_id)){//如果 该会员没有该产品包
                    $response = Lang::get("scs_error.order_ac_product_error_400140");
                    break;
                }
            }

            $pap=ProductActivityPackage::where('product_activity_id',Input::get('id'))->select('package_id',"id")->get()->toArray();
            $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            if(is_null($whp)){
                $response = Lang::get("scs_error.work_hour_error_10001");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $response=OrderProduct::addProductActivity($pap,$sc_user,$orders,$wmcf,$whp,$pa,$csi);
        }while(false);
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-7 17:12:52
     * @Description: 删除活动包
     */
    public function deleteProductActivity(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                "order_product_id"  => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$delete_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            } 
            $order_product=OrderProduct::find(Input::get("order_product_id"));
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $response=OrderProduct::deleteActivityProducts($order_product,$sc_user->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getProductPackages", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-9 13:37:41
     * @Description: 用于更改活动包产品数量
     */
    public function changeAcItemNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opaci=OrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id||$opaci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=OrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=OrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opaci->num&&$amount==$opaci->amount){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=OrderProductAcItem::changeProductItemNum($order_product,$opaci,$opacp,$number,$amount,$sc_user->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getProductPackages", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-9 14:52:58
     * @Description: 更换活动包配件
     */
    public function changeAcProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opaci=OrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id||$opaci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=OrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=OrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            // $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            // if(is_null($whp)){
            //     $response = Lang::get("scs_error.work_hour_error_10001");
            //     break;
            // }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $si=SupplyItem::find(Input::get("new_item_id"));

            $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
            $wmcf->supply_coefficient = null;
            if (!empty($wpc)) {
                $wpc_city=WorkPartsCoefficientCity::where('city_id',$orders->city)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
            }
            if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
            }
    
            //要更换成的配件类型判断
            if($si->type==0){
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opaci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItem::find(Input::get("new_item_id"));
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }        
                $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
    
                // if(Orders::isSale($si->id,0)){
                //     $response = Lang::get("scs_error.supply_error_10011");
                //     break;
                // }
                //                 $spi=SupplyProductItem::where("car_model_id",$csi->id)->where("supply_item_id",$si->id)->where("status",1)->first();
                //                 if(is_null($spi)){
                //                     $response = Lang::get("scs_error.supply_item_car_model_error_41001");
                //                     break;
                //                 }
    
            }else{
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                if(!$mw || $opaci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
                
                $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            }
            if(Orders::isSale($si->id,$si->type)){
                $response = Lang::get("scs_error.supply_error_10011");
                break;
            }
    
            //原有配件类型判断
            // if($opaci->item_type==0){
                $old_si=SupplyItem::find($opaci->item_id);
            // }else{
            //     $old_si=SupplyItemNonStandard::find($opaci->item_id);
            // }
//             if(is_null($si)||$si->status!=1){
//                 $response = Lang::get("scs_error.supply_item_not_exit_41000");
//                 break;
//             }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                $temp_subject=($opaci->temp_subject==0)?$old_si->supply_classification_id:$opaci->temp_subject;//如果
                if($temp_subject!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断
            if($opaci->temp_subject==0){
                $opaci->temp_subject=$old_si->supply_classification_id;
            }
    
            // $number=Input::get("number");
            // if($number==$opbi->num){
            //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
            //     break;
            // }
            $response=OrderProductAcItem::changeProductItem($order_product,$opaci,$opacp,$si,$sc_user->id,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-9 15:35:17
     * @Description: 用于无适配耗材更换基础产品包的耗材
     */
    public function changeNoAdapterAcProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opaci=OrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=OrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=OrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            // $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            // if(is_null($whp)){
            //     $response = Lang::get("scs_error.work_hour_error_10001");
            //     break;
            // }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $si=SupplyItem::find(Input::get("new_item_id"));
            $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
            $wmcf->supply_coefficient = null;
            if (!empty($wpc)) {
                $wpc_city=WorkPartsCoefficientCity::where('city_id',$orders->city)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
            }
            if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
            }
    
            //要更换成的配件类型判断
            if($si->type==0){
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                if(!$mw || $opaci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }        
                $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
    
               
                //                 $spi=SupplyProductItem::where("car_model_id",$csi->id)->where("supply_item_id",$si->id)->where("status",1)->first();
                //                 if(is_null($spi)){
                //                     $response = Lang::get("scs_error.supply_item_car_model_error_41001");
                //                     break;
                //                 }
    
            }else{
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opaci->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
                // if(Orders::isSale($si->id,1)){
                //     $response = Lang::get("scs_error.supply_error_10011");
                //     break;
                // }
                $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            }
            if(Orders::isSale($si->id,$si->type)){
                $response = Lang::get("scs_error.supply_error_10011");
                break;
            }
    
//             if(is_null($si)||$si->status!=1){
//                 $response = Lang::get("scs_error.supply_item_not_exit_41000");
//                 break;
//             }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($opaci->item_id!=$si->supply_classification_id){//在添加无适配耗材时耗材id存入的是品类id
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
    
            $opaci->temp_subject=$opaci->item_id;
    
            // $number=Input::get("number");
            // if($number==$opbi->num){
            //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
            //     break;
            // }
            $response=OrderProductAcItem::changeProductItem($order_product,$opaci,$opacp,$si,$sc_user->id,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-2-24 14:01:52
     * @Description: 用于添加 工时
     */
    public function addWorkHours(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'order_product_id'=> 'required',
                'amount' => 'required|numeric|min:0'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            if($orders->order_type==1){
                $csi=DataCarStandardInfo::find($orders->car_model_id);
                if(is_null($csi)){//只进行 车型存在 不对车型状态进行祥判断
                    $response = Lang::get("scs_error.order_car_model_notexit_40002");
                    break;
                }
            }
            $op=OrderProduct::find(Input::get('order_product_id'));
            if(is_null($op)||$op->product_type!='non_standard'){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            $opns=OrderProductNonStandard::where("order_product_id",$op->id)->where("service_type",2)->where('status',1)->first();
            if($opns){
                $response = Lang::get("scs_error.order_product_work_exit_40032");
                break;
            }
            $opns=new OrderProductNonStandard;
            $opns->order_id=$orders->order_id;
            $opns->name='工时费';
            $opns->num=0;
            $opns->amount=Input::get('amount')*100;
            $opns->order_product_id=Input::get('order_product_id');
            $opns->work_station=0;
            $opns->work_status="prepare";
            $opns->type=1;
            $opns->unit='个';
            $opns->service_type=2;
            $opns->save();
    
            //每次添加标准或非标配件都需修改order_product及orders中金额
            $op->amount+=Input::get('amount')*100;
            $op->save();
    
            $orders->amount+=Input::get('amount')*100;
            $orders->balance_amount+=Input::get('amount')*100;
            $orders->save();
            $opr=new OrderProductRecord();
            $opr->order_id=$orders->order_id;
            $opr->product_type='non_standard';
            $opr->operation_type='create';
            $opr->num=0;
            $opr->operation_grade='二级';
            $opr->user_id=$sc_user->id;
            $opr->user_type='sc';
            $opr->operation_id=$opns->id;
            $opr->name=$opns->name;
            $opr->amount=$opns->amount;
            $opr->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-2-24 14:01:52
     * @Description: 用于修改工时费
     */
    public function updateWorkHours(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'item_id'=> 'required',//order_product_non_standard的id
                'amount' => 'required|numeric'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            if($orders->order_type==1){
                $csi=DataCarStandardInfo::find($orders->car_model_id);
                if(is_null($csi)){//只进行 车型存在 不对车型状态进行祥判断
                    $response = Lang::get("scs_error.order_car_model_notexit_40002");
                    break;
                }
            }
            $opns=OrderProductNonStandard::where('id',Input::get('item_id'))->where('status',1)->first();
            $op=OrderProduct::find($opns->order_product_id);
    
            if($opns->order_id!=Input::get('order_id')){
                $response = Lang::get("scs_error.supply_subject_error_41003");
                break;
            }
    
            $opr=new OrderProductRecord();
            $opr->order_id=$orders->order_id;
            $opr->product_type='non_standard';
            $opr->operation_type='update';
            $opr->num=0;
            $opr->operation_grade='二级';
            $opr->user_id=$sc_user->id;
            $opr->user_type='sc';
            $opr->operation_id=$op->id;
            $opr->name=$opns->name;
            $opr->amount=$opns->amount;
            $opr->save();
    
            $old_amount=$opns->amount;
            $discount=0;
            if($opns->discount!=1){
                $discount=(Input::get('amount')*100-$opns->amount)*(1-$opns->discount);//多优惠的金额
            }
            $opns->amount=Input::get('amount')*100;
            $opns->save();
    
            //每次添加标准或非标配件都需修改order_product及orders中金额
            $op->amount=$op->amount+Input::get('amount')*100-$old_amount;
            $op->discount=$op->discount+$discount;
            $op->save();
    
            $orders->amount=$orders->amount+Input::get('amount')*100-$old_amount;
            $orders->balance_amount=$orders->balance_amount+Input::get('amount')*100-$old_amount-$discount;
            $orders->discount=$orders->discount+$discount;
            $orders->save();
    
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:zhanghu
     * @Description:保存门店对企业用户的行动方案
     */
    public function storeShopsEsPlan(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'user_id'=> 'required',//
            )
        );
        $response = array('code' => 0, 'msg' => '保存成功');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                $response = array('code' => -1, 'msg' => '登录验证失败');
                break;
            }
            $id = Input::get('id');
            if($id) {
                $shopty = ShopUserProperty::find($id);
                if(!$shopty){
                    $response = array('code' => -1, 'msg' => '找不到这个方案');
                    break;
                }
            }else{
                $shopty = new ShopUserProperty();
            }
            $shopty->shop_id = Input::get('shop_id');
            $shopty->user_id = Input::get('user_id');
            $shopty->user_type = 2;
            $shopty->user_comments = Input::get('user_comments');
            $shopty->created_by = $sc_user->id;
            try {
                $shopty->save();
            }catch(Exception $e){
                $response = array('code' => -1, 'msg' => '保存失败');
                break;
            }
            $response["result"] = $shopty;
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "storeShopsEsPlan", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:zhanghu
     * @Description:获取门店对企业用户的行动方案
     */
    public function getShopsEsPlan(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'user_id'=> 'required',//
            )
        );
        $response = array('code' => 0, 'msg' => 'ok',"result"=>array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["code"] = -1;
                $response["result"] = $validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                $response = array('code' => -1, 'msg' => '登录验证失败');
                break;
            }
            $shopty = ShopUserProperty::where('shop_id',Input::get('shop_id'))
            ->where('user_id',Input::get('user_id'))
            ->where('user_type',2)
            ->first();
            if(!$shopty){
                $response = array('code' => 0, 'msg' => 'ok',"result" => new stdClass());
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok',"result"=>$shopty);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "storeShopsEsPlan", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    
    /**
     * @Author:    zwh
     * @DateTime:  2017-4-17 11:22:46
     * @Description:修改前判断是否可以进入更换弹层
     */
    public function changeAcProductItemBefore(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',//订单耗材的id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opaci=OrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id||$opaci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=OrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=OrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            // $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            // if(is_null($whp)){
            //     $response = Lang::get("scs_error.work_hour_error_10001");
            //     break;
            // }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $moo=MaintenanceOutOrder::where("product_type",$order_product->product_type)->where("product_item_id",$opaci->id)->whereIn("status",array(0,1,2))->first();
            if($moo){//如果存在 备货中或者 等待领料的 出库申请 已经领料的 不允许修改
                $response = Lang::get("scs_error.order_product_item_change_error_40012");
                break;
            }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
}